<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%@ page import="java.sql.*"%>
<%@ page import="com.javabean.*"%>
<%@ page import="com.model.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title></title>
<link href="../css/css.css" rel="stylesheet" type="text/css" />
<link href="../css/style.css" rel="stylesheet" type="text/css" />
<script src="../js/jquery-1.9.0.js"></script>
<style type="text/css">
a.import:link {text-decoration:none;}
a.import:visited {text-decoration:none;}
a.import:hover {text-decoration:none;}
a.import:active {text-decoration:none;}
</style>
<script language="JavaScript" type="text/javascript">
	function selectAll() {
		var obj = document.fom.elements;
		for ( var i = 0; i < obj.length; i++) {
			if (obj[i].name == "delid") {
				obj[i].checked = true;
			}
		}
	}

	function unselectAll() {
		var obj = document.fom.elements;
		for ( var i = 0; i < obj.length; i++) {
			if (obj[i].name == "delid") {
				if (obj[i].checked == true)
					obj[i].checked = false;
				else
					obj[i].checked = true;
			}
		}
	}

	function link1() {
		document.getElementById("fom").removeAttribute("enctype");
		document.getElementById("fom").action = "WP_In.jsp";
		document.getElementById("fom").method = "get";
		document.getElementById("fom").submit();
	}

	function itemImport() {
		document.getElementById("fom").enctype = "multipart/form-data";
		document.getElementById("fom").method = "post";
		document.getElementById("fom").action = "ItemDeal?flag=import";
		document.getElementById("fom").submit();
	}

	function itemExport() {
		document.getElementById("fom").removeAttribute("enctype");
		document.getElementById("fom").method = "post";
		document.getElementById("fom").action = "ItemDeal?flag=export";
		document.getElementById("fom").submit();
	}

	function go() {
		if ($("#textfield3").val() == "" || null) {
			alert("请输入页数！");
			return;
		}
		var jump_page = document.getElementById("jump_page").value;
		location.href = "?pageCar=" + jump_page;
	}
</script>
</head>

<body>
	<%
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");

		int pageNow = 1;
		int pagePrev = 0;
		int pageNext = 0;
		int entityCount = 0;
		int pageSize = 10; //	the number of entities one page contains
							//	set as variable but not constant for future
							//	user-defined function.
		int pageTotal = 0;
		String query = request.getParameter("query");

		String pageNowStr = request.getParameter("pageItem");
		if (pageNowStr != null)
			pageNow = Integer.parseInt(pageNowStr);
		else
			; // initial value is 1

		Connection con = DataBase.getConnection();
		PreparedStatement pstmtCount = con
				.prepareStatement("SELECT COUNT(*) as count FROM item");
		ResultSet rs = pstmtCount.executeQuery();
		if (rs.next()) {
			entityCount = rs.getInt("count");
			int mod = entityCount % pageSize;
			if (mod == 0)
				pageTotal = entityCount / pageSize;
			else
				pageTotal = entityCount / pageSize + 1;
		} else {
			pageTotal = 1;
		}
		rs.close();

		if (pageNow > pageTotal || pageNow < 1)
			pageNow = 1;

		if (pageNow > 1)
			pagePrev = pageNow - 1;
		else
			; // initial value is 0

		if (pageNow < pageTotal)
			pageNext = pageNow + 1;
		else
			; // initial value is 0
		int userGrade = ((User) request.getSession(true).getAttribute(
				"user")).getUserGrade();
		boolean isAuthority = false;
		if (userGrade != 0 && userGrade != 1 && userGrade != 4
				&& userGrade != 5 && userGrade != 8 && userGrade != 9
				&& userGrade != 12 && userGrade != 13) {
			isAuthority = true;
		}
	%>
	<form name="search" id="search" method="get" action="listWP.jsp">
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
			<tr>
				<td height="62" background="../image/widgetbg5.jpg">

					<table width="98%" border="0" align="center" cellpadding="0"
						cellspacing="0">
						<tr>
							<td width="24"><img src="../images/ico07.gif" width="20"
								height="18" /></td>
							<td width="550"><label> 物品关键词: <input name="query"
									type="text" />
							</label> <input name="Submit" type="button"
								onclick="document.getElementById('search').submit();"
								value="查 询" /></td>
						</tr>
					</table>
				</td>
			</tr>
		</table>
	</form>
	<%
		
	%>
	<form name="fom" id="fom">
		<table width="100%" border="0" cellspacing="0" cellpadding="0">
			<tr>
				<td>
					<table id="subtree1" style="DISPLAY:" width="100%" border="0"
						cellspacing="0" cellpadding="0">
						<tr>
							<td><table width="95%" border="0" align="center"
									cellpadding="5" cellspacing="0">
									<tr>
										<td height="20"><span class="newfont07">选择：<a
												href="#" class="right-font08" onclick="selectAll();">全选</a>-<a
												href="#" class="right-font08" onclick="unselectAll();">反选</a></span>
											<%
												if (isAuthority) {
											%> <input name="Submit" type="button" value="入库"
											onclick="link1();" /> <input
											style="wdith: 200px; border: solid 1px #eee"
											name="excelContent" id="excelContent" type="file"
											hidden="hidden" onchange="itemImport();" />
											<a class="import" title="物品导入支持xls格式的批量物品信息导入功能，&#10格式为‘名称，品牌，数量，单位，库房名称，部门，经手人’，&#10注意库房名称必须已经存在！&#10数据列不应为空，且应从第一行至最后一行存放">
											<input name="Submit2" type="button" value="物品导入"
											onclick="document.getElementById('excelContent').click();" /></a>
											<a class="import" title="导入格式样例下载" href="../xls/item.xls"><img width="15" height="15" alt="导入格式样例下载" src="../images/help.png"/></a>
											<input name="Submit3" type="button" value="物品导出"
											onclick="itemExport();" />
 											<%}%>
 										</td>
									</tr>
									<tr>
										<td height="40" class="font42"><table width="100%"
												border="0" cellpadding="4" cellspacing="1" bgcolor="#464646"
												class="newfont03">
												<tr>
													<td align="center" height="20" colspan="5" align="right"
														class="tablestyle_title">物品信息列表</td>
												</tr>
												<tr>
													<td width="5%" align="center" class="zsd">选择</td>
													<td width="40%" height="20px" align="center" class="zsd">物品名称（单位）</td>
													<td width="15%" align="center" class="zsd">库存数量</td>
													<td width="15%" align="center" class="zsd">存放位置</td>
													<td width="20%" align="center" class="zsd">操作</td>
												</tr>
												<%
													int entityNumBegin = pageSize * (pageNow - 1);
													PreparedStatement pstmtStore = con
															.prepareStatement("SELECT room FROM store_room WHERE name=?");
													PreparedStatement pstmtEntities = null;
													if (query == null)
														pstmtEntities = con
																.prepareStatement("SELECT * FROM item WHERE amount>0 ORDER BY brand,name LIMIT "
																		+ pageSize * (pageNow - 1) + "," + pageSize);

													else {
														pstmtEntities = con
																.prepareStatement("SELECT * FROM item WHERE amount>0 AND (name LIKE ? OR brand LIKE ? OR store LIKE ? OR description LIKE ? OR person LIKE ?) ORDER BY brand,name LIMIT "
																		+ pageSize * (pageNow - 1) + "," + pageSize);
														pstmtEntities.setString(1, "%" + query + "%");
														pstmtEntities.setString(2, "%" + query + "%");
														pstmtEntities.setString(3, "%" + query + "%");
														pstmtEntities.setString(4, "%" + query + "%");
														pstmtEntities.setString(5, "%" + query + "%");
													}
													rs = pstmtEntities.executeQuery();

													while (rs.next()) {
														pstmtStore.setString(1, rs.getString("store"));
														ResultSet rsStore = pstmtStore.executeQuery();
												%>
												<tr>
													<td bgcolor="#FFFFFF" align="center"><input
														type="checkbox" name="delid" /></td>
													<td height="20" bgcolor="#FFFFFF" align="center"><a
														href="WP_CK.jsp?iid=<%=rs.getLong("iid")%>"><%=rs.getString("brand") + rs.getString("name")%></a></td>
													<td bgcolor="#FFFFFF" align="center"><%=rs.getString("amount") + " " + rs.getString("unit")%></td>
													<td bgcolor="#FFFFFF" align="center"><%=rs.getString("store")
						+ (rsStore.next() ? "(" + rsStore.getString("room")
								+ ")" : "该仓库已不存在")%></td>
													<%
														if (isAuthority) {
													%>
													<td bgcolor="#FFFFFF" align="center"><a
														href="<%="WP_BJ.jsp?iid=" + rs.getLong("iid")%>">编辑</a>&nbsp;|&nbsp;<a
														href="<%="WP_CK.jsp?iid=" + rs.getLong("iid")%>">查看</a>&nbsp;|&nbsp;<a
														href="<%="WP_In.jsp?iid=" + rs.getLong("iid")%>">入库</a>&nbsp;|&nbsp;<a
														href="<%="WP_Out.jsp?iid=" + rs.getLong("iid")%>">出库</a></td>
												</tr>
												<%
													} else {
												%><tr>
													<td bgcolor="#FFFFFF" align="center"><a
														href="<%="WP_CK.jsp?iid=" + rs.getLong("iid")%>">查看</a></td>
												</tr>
												<%
													}
												%>
												<%
													rsStore.close();
													}
													rs.close();
												%>
											</table></td>
									</tr>
								</table></td>
						</tr>
					</table>
					<table width="95%" border="0" align="center" cellpadding="0"
						cellspacing="0">
						<tr>
							<td height="33"><table width="100%" border="0"
									align="center" cellpadding="0" cellspacing="0"
									class="right-font08">
									<tr>
										<td width="50%">共 <span class="right-text09"><%=pageTotal%></span>
											页 | 第 <span class="right-text09"><%=pageNow%></span> 页
										</td>
										<td width="49%" align="right">[<a href="?pageItem=1"
											class="right-font08">首页</a> | <a
											href="<%="?pageItem=" + ((pagePrev == 0) ? pageTotal : pagePrev)%>"
											class="right-font08">上一页</a> | <a
											href="<%="?pageItem=" + ((pageNext == 0) ? 1 : pageNext)%>"
											class="right-font08">下一页</a> | <a
											href="<%="?pageItem=" + pageTotal%>" class="right-font08">末页</a>]
											转至：
										</td>
										<td width="1%"><table width="20" border="0"
												cellspacing="0" cellpadding="0">
												<tr>
													<td width="1%"><input id="textfield3"
														name="textfield3" type="text" class="right-textfield03"
														size="1" id="jump_page" /></td>
													<td width="87%"><input name="Submit23222"
														type="button" class="right-button06" value=" "
														onclick="go()" /></td>
												</tr>
											</table></td>
									</tr>
								</table></td>
						</tr>
					</table>
				</td>
			</tr>
		</table>
	</form>

	<%
		con.close();
	%>
</body>
</html>
